<?php
	if(!defined('LT2N_SYS'))
	{
		die("Bạn không có quyền truy cập !");
	}
	$db = new Model();
	if($_GET["mod"] == "user")
	{
		if($_GET["act"] == "register")
		{
			if(isset($_POST["btnthemtaikhoan"]))
			{
				$username = $_POST["txtusername"];
				$pass = md5($_POST["txtpass"]);
				$firstname = $_POST["firstname"];
				$lastname = $_POST["lastname"];
				$sex = $_POST["sex"];
				$email = $_POST["txtemail"];

				$arr_user = array(
					"username" => $username,
					"password" => $pass,
					"firstname" => $firstname,
					"lastname" => $lastname,
					"sex" => $sex,
					"email" => $email,
					"add_time" => nvs_createDate(date('d-m-Y h:i'),"dd-mm-yyyy h:i"),
					"status" => 0
				);
					$db->insert("user",$arr_user);
					$thongbao = $db->check(array(">> Đăng ký thành công"=>">> Đăng ký thất bại."));
			}
			require_once("views/user/register.php");
		}
		elseif($_GET["act"] == "login")
		{

			$username = $_POST["txttaikhoan"];
			$pass = md5($_POST["txtmatkhau"]);
			if($_POST["tbnDangnhap"])
			{
				$arr = $db->select("user","username = '".$username."' and password ='".$pass."' ");
				//echo $db->checkQuery();
				if($db->checkQuery() == 1)
				{
					$_SESSION["username"]= $username;

					header('location:'.LT2N_BASEURL);
				}
				else
				{
					$loi = "<strong style='color:#000; font-size:20px;'>&raquo;&nbsp;</strong> Sai mật khẩu hoặc tài khoản không tồn tại !";
				}
			}
			require_once("views/user/login.php");
		}
		elseif($_GET["act"] == "manage")
		{
			$arr_user_session = $db->select("user","username = '" .$_SESSION["username"] . "' ");
			$arr_file = $db->select("attach","username = '" .$_SESSION["username"] . "' ");
			require_once("views/user/manage.php");
		}
		elseif($_GET["act"] == "edit")
		{
			$arr_edit = $db->select("user","username = '" .$_SESSION["username"] . "' ");
			if(isset($_POST["btnthemtaikhoan"]))
			{
				$pass = $arr_edit[0]["password"];
				if($_POST["txtpass"] != "")
				{
					$pass = md5($_POST["txtpass"]);
				}
				$firstname = $_POST["firstname"];
				$lastname = $_POST["lastname"];
				$sex = $_POST["sex"];
				$email = $_POST["txtemail"];

				$arr_user = array(
					"password" => $pass,
					"firstname" => $firstname,
					"lastname" => $lastname,
					"sex" => $sex,
					"email" => $email,
					"add_time" => $arr_edit[0]["add_time"],
				);
					$db->update("user",$arr_user, "username = '". $_SESSION["username"] ."' ");
					$thongbao = $db->check(array(">> Đăng ký thành công"=>">> Đăng ký thất bại."));
			}
			require_once("views/user/edit.php");
		}
	}
?>